package com.chzc.fm.duotin.adapter.recyclerview;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.bean.mine.FollowBean;
import com.chzc.fm.duotin.utils.ImageUtil;
import com.chzc.fm.duotin.utils.NumberUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by BillSinla on 2016/7/1.
 */
public class FollowRecyclerAdapter extends RecyclerView.Adapter<FollowRecyclerAdapter.Holder>{

    private Context context;
    private List<FollowBean> followBeanList;
    private LayoutInflater inflater;
    private SpannableString spanString;
    private OnItemClickListener onItemClickListener;

    public FollowRecyclerAdapter(Context context, List<FollowBean> followBeanList) {
        this.context = context;
        this.followBeanList = followBeanList;
        inflater = LayoutInflater.from(context);
        init();
    }

    private void init() {
        spanString = new SpannableString(" 1");
        Drawable d = context.getResources().getDrawable(R.drawable.ic_vip_view);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        ImageSpan imageSpan = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
        spanString.setSpan(imageSpan, 1, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.adapter_follow_item, null);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        ImageLoader.getInstance().displayImage(followBeanList.get(position).getImage_url(), holder.imageAvatar, ImageUtil.getCircleOptions());

        holder.textTitle.setText(followBeanList.get(position).getReal_name());

        holder.textDes.setText(followBeanList.get(position).getDescribe());

        int num = Integer.parseInt(followBeanList.get(position).getFollowed_num());
        holder.textFans.setText("粉丝数：" + NumberUtil.countNumber(num));

        if(followBeanList.get(position).getIs_v().equals("1")){
            holder.textTitle.append(spanString);
        }

        final int flag = position;
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onClick(flag);
            }
        });
    }

    @Override
    public int getItemCount() {
        return followBeanList != null ? followBeanList.size() : 0;
    }

    protected class Holder extends RecyclerView.ViewHolder{
        public ImageView imageAvatar;
        public TextView textTitle, textDes, textFans;
        public Holder(View itemView) {
            super(itemView);
            imageAvatar = (ImageView) itemView.findViewById(R.id.iv_follow_avatar);
            textTitle = (TextView) itemView.findViewById(R.id.tv_follow_name);
            textDes = (TextView) itemView.findViewById(R.id.tv_follow_des);
            textFans = (TextView) itemView.findViewById(R.id.tv_follow_fans);
        }
    }
    public interface OnItemClickListener{
        void onClick(int position);
    }
}
